/**
 * 渠道来源分析
 */
define(function (require, exports, module) {
    var $ = require("jquery");
    var moment = require("moment");
    var angular = require("angular");
    require("angular-route");
    require("angular-ui");
    require("angular-interceptor");
    require("./project_tree");
    var util = require("util");
    require('global');

    var app = angular.module('app', ['ngRoute', 'ngUI', 'ngInterceptor','projectTreeSelector']);
    app.service('queryService', ['$http', function ($http) {

        var service = {
            getPorjList: function () {
                return $http.get('/api/index.php?r=qdgl/common/project-list');
            },
            getSourceData: function (params) {
                return $http.get('/api/index.php?r=statistics/qdgl-statistics/get-project-clue-statistics'+'&'+ params);
            }
        };

        return service;


    }]);


    function MainController($scope, $location, service) {

        $scope.showRule = false;


        service.getPorjList().then(function (res) {
            if (res.data.retCode == 0 && res.data.data.length>0) {
                $scope.projectList = res.data.data;
            }
        });


        $scope.setDateMode = function (mode) {
            $scope.query.dateMode = mode;

            var start, end;
            var today = moment().startOf('day').subtract(1);

            switch (mode) {
                case 'year':
                    start = moment().startOf('day').subtract(1, 'day').startOf('year');
                    end = moment(start).add(1, 'years').subtract(1);
                    if (end.isAfter(today)) {
                        end = today;
                    }
                    break;

                case 'quarter':
                    start = moment().startOf('day').subtract(1, 'day').startOf('quarter');
                    end = moment(start).add(1, 'quarters').subtract(1);
                    if (end.isAfter(today)) {
                        end = today;
                    }
                    break;

                case 'month':
                    start = moment().startOf('day').subtract(1, 'day').startOf('month');
                    end = moment(start).add(1, 'months').subtract(1);
                    if (end.isAfter(today)) {
                        end = today;
                    }
                    break;

                case 'week':
                    start = moment().startOf('day').subtract(1, 'day').startOf('week');
                    end = moment(start).add(1, 'weeks').subtract(1);
                    if (end.isAfter(today)) {
                        end = today;
                    }
                    break;

                case 'custom':
                    return;
            }

            if (start) {
                $scope.query.startDate = start.format('YYYY-MM-DD');
            } else {
                $scope.query.startDate = '';
            }
            if (end) {
                $scope.query.endDate = end.format('YYYY-MM-DD');
            } else {
                $scope.query.endDate = '';
            }
        };


        var query = {
            dateMode: 'month',
            startDate: null,
            endData: null,
            query: function () {
                service.getSourceData($.param({
                    proj_id: $scope.curProjectId, start_time: query.startDate,
                    end_time: query.endDate
                })).then(function (res) {
                    if (res.data.retCode == 0) {
                        $scope.sourceData = res.data.data;
                    }
                })
            },
            exportData: function () {
                var filters = {
                    proj_id: $scope.curProjectId,
                    start_time: query.startDate,
                    end_time: query.endDate
                };
                var url = '/api/index.php?r=statistics/qdgl-statistics/export-project-clue-statistics&'+ $.param(filters);
                util.download(url);
            }

        };

        $scope.sourceData = {
            follow_level_structure: [],
            data: []
        };
        $scope.query = query;

        $scope.setDateMode('month');

        $scope.$watch('curProjectId',function(newValue){
            if ($scope.curProjectId) {
                $scope.query.query();
            }
        });


    }

    app.controller('MainController', ['$scope', '$location', 'queryService', MainController]);


    angular.bootstrap(document, ['app']);
});